package com.edu.controller;

import com.edu.dao.NewsMapper;
import com.edu.data.News;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    NewsMapper newsMapper=sqlSession.getMapper(NewsMapper.class);

    @RequestMapping("/toNews")
    private void toNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        List<News> news = newsMapper.AllNews();
        req.setAttribute("news", news);
        req.getRequestDispatcher("../static/manage/news.jsp").forward(req, resp);
    }


    @RequestMapping("/readNews")
    private void readNews(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        String entityId=req.getParameter("entityId");
        News news=newsMapper.findById(Long.parseLong(entityId));
        System.out.println(news.getEnContent());
        req.setAttribute("entityId", Long.toString(news.getEnId()));
        req.setAttribute("title", news.getEnTitle());
        req.setAttribute("content", news.getEnContent());
        req.getRequestDispatcher("../static/manage/news-modify.jsp").forward(req,resp);
    }

    @RequestMapping("/createNews")
    private void createNews(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("../static/manage/news-modify.jsp");
    }

    @RequestMapping("/delNews")
    private void delNews(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        newsMapper.delete(Long.parseLong(req.getParameter("entityId")));
        resp.sendRedirect("toNews");
    }

    @RequestMapping("/saveNews")
    private void saveNews(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String entityId = req.getParameter("entityId");
        News news;
        if (entityId.isEmpty()){
            news = new News();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            news.setEnCreateTime(df.format(new Date()));
            news.setEnTitle(req.getParameter("title"));
            news.setEnContent(req.getParameter("content"));
            newsMapper.insert(news);
        }else {
            news = newsMapper.findById(Long.parseLong(entityId));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            news.setEnCreateTime(df.format(new Date()));
            news.setEnTitle(req.getParameter("title"));
            news.setEnContent(req.getParameter("content"));
            newsMapper.update(news);
        }
        resp.sendRedirect("toNews");
    }
}
